package de.unikoblenz.west.csxpoi;

import de.unikoblenz.west.csxpoi.R;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * The activity for inspecting POIs.
 */
public class PoiInspectorActivity extends Activity {

	/**
	 * the inspected POI
	 */
	private PoiWrapper mPoi = null;

	/**
	 * the inspecting user
	 */
	private String mUserId = null;

	/**
	 * a flag whether the POI has been edited
	 */
	private boolean mEditedFlag = false;

	/**
	 * the constant representing the POI editor request
	 */
	private static final int POI_EDIT_REQUEST = 1;

	/**
	 * Called when the activity is started.
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.poi_inspector);

		Bundle extras = getIntent().getExtras();

		mUserId = extras.getString("user");
		mPoi = extras.getParcelable("poi");
		mEditedFlag = false;

		// Edit button:
		final Button editButton = (Button) findViewById(R.id.edit_button);
		if (mUserId != null) {
			editButton.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					Intent poiEditorIntent = new Intent(
							PoiInspectorActivity.this, PoiEditorActivity.class);
					poiEditorIntent.putExtra("user", mUserId);
					poiEditorIntent.putExtra("poi", mPoi);
					startActivityForResult(poiEditorIntent, POI_EDIT_REQUEST);
				}
			});
		} else {
			editButton.setEnabled(false);
		}

		// Close button:
		final Button closeButton = (Button) findViewById(R.id.close_button);
		closeButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent resultIntent = new Intent(PoiInspectorActivity.this,
						CsxPoiActivity.class);
				if (mEditedFlag)
					resultIntent.putExtra("poi", mPoi);
				setResult(RESULT_OK, resultIntent);
				finish();
			}
		});
		updateView();
	}

	/**
	 * Updates the variable parts of the view.
	 */
	private void updateView() {
		final TextView nameView = ((TextView) findViewById(R.id.name));
		nameView.setText(mPoi.getName());

		// event text view
		String eventText = "";
		final TextView eventTextView = (TextView) findViewById(R.id.EventTextView);
		final TextView eventHeading = (TextView) findViewById(R.id.EventHeading);
		if (!mPoi.getStartDate().equals("")) {
			eventText += "from \t"
					+ Helpers.formatIsoStringToNiceFormat(mPoi.getStartDate())
					+ "\n";
		}
		if (!mPoi.getEndDate().equals("")) {
			eventText += "until \t"
					+ Helpers.formatIsoStringToNiceFormat(mPoi.getEndDate());
		}
		if (!eventText.equals("")) {
			eventTextView.setText(eventText);
			eventTextView.setVisibility(View.VISIBLE);
			eventHeading.setVisibility(View.VISIBLE);
		} else {
			eventTextView.setVisibility(View.GONE);
			eventHeading.setVisibility(View.GONE);
		}

		// Categories:
		final LinearLayout categoryItemsContainer = (LinearLayout) findViewById(R.id.category_items_container);
		categoryItemsContainer.removeAllViews();
		for (final CategoryWrapper category : mPoi.getCategories()) {
			final TextView categoryItemView = (TextView) View.inflate(
					PoiInspectorActivity.this, R.layout.category_item, null);
			categoryItemView.setText(category.getName());
			if (mUserId != null) {
				// categoryItemView.setTextColor(Color.rgb(0, 0, 185));
				categoryItemView.setText(Helpers.getUnderlinedString(category
						.getName()));

				categoryItemView.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						Intent ontologyEditorIntent = new Intent(
								PoiInspectorActivity.this,
								OntologyEditorActivity.class);
						ontologyEditorIntent.putExtra("user", mUserId);
						ontologyEditorIntent.putExtra("uri", category.getUri());
						ontologyEditorIntent.putExtra("name", category
								.getName());
						startActivity(ontologyEditorIntent);
					}
				});
			}
			categoryItemsContainer.addView(categoryItemView);
		}

		if (mPoi.getCategories().isEmpty()) {
			final TextView categoryItemView = (TextView) View.inflate(
					PoiInspectorActivity.this, R.layout.category_item, null);
			categoryItemView.setText("none");
			categoryItemView.setTypeface(Typeface.DEFAULT, Typeface.ITALIC);
			categoryItemsContainer.addView(categoryItemView);
		}
	}

	/**
	 * Called when an activity returns a result.
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		Bundle extras = null;
		if (data != null)
			extras = data.getExtras();
		switch (requestCode) {
		case POI_EDIT_REQUEST:
			if (resultCode == RESULT_OK && extras != null) {
				mPoi = extras.getParcelable("poi");
				mEditedFlag = true;
				if (mPoi.selfUpdate()) {
					updateView();
				} else {
					Intent resultIntent = new Intent(PoiInspectorActivity.this,
							CsxPoiActivity.class);
					resultIntent.putExtra("poi", mPoi);
					setResult(RESULT_OK, resultIntent);
					finish();
				}
			}
			break;
		}
	}
}
